package com.java110.common.bmo.appraise.impl;

import com.java110.common.bmo.appraise.ISaveAppraiseBMO;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.repair.RepairDto;
import com.java110.dto.repair.RepairSettingDto;
import com.java110.dto.repair.RepairUserDto;
import com.java110.intf.common.IAppraiseInnerServiceSMO;
import com.java110.intf.community.IRepairInnerServiceSMO;
import com.java110.intf.community.IRepairSettingInnerServiceSMO;
import com.java110.intf.community.IRepairUserInnerServiceSMO;
import com.java110.po.appraise.AppraisePo;
import com.java110.po.owner.RepairPoolPo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("saveAppraiseBMOImpl")
public class SaveAppraiseBMOImpl implements ISaveAppraiseBMO {

    @Autowired
    private IAppraiseInnerServiceSMO appraiseInnerServiceSMOImpl;

    @Autowired
    private IRepairInnerServiceSMO repairInnerServiceSMOImpl;

    @Autowired
    private IRepairUserInnerServiceSMO repairUserInnerServiceSMOImpl;

    @Autowired
    private IRepairSettingInnerServiceSMO repairSettingInnerServiceSMOImpl;
    /**
     * 校验入参
     *
     * @param appraisePo
     */
    private void validate(AppraisePo appraisePo) {

        Assert.hasKeyAndValue(appraisePo, "appraiseScore", "未包含评分");
        Assert.hasKeyAndValue(appraisePo, "appraiseType", "未包含评价类型");
        Assert.hasKeyAndValue(appraisePo, "context", "未包含评价内容");
        Assert.hasKeyAndValue(appraisePo, "appraiseUserId", "未包含评价者");
        Assert.hasKeyAndValue(appraisePo, "appraiseUserName", "未包含评价者名称");
        Assert.hasKeyAndValue(appraisePo, "objType", "未包含评价对象类型");
        Assert.hasKeyAndValue(appraisePo, "objId", "未包含评价对象ID");

    }


    @Override
    @Java110Transactional
    public ResponseEntity<String> saveAppraise(AppraisePo appraisePo) {
        validate(appraisePo);
        if (StringUtil.isEmpty(appraisePo.getAppraiseId()) || appraisePo.getAppraiseId().startsWith("-")) {
            appraisePo.setAppraiseId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_appraiseId));
        }

        if (StringUtil.isEmpty(appraisePo.getParentAppraiseId())) {
            appraisePo.setParentAppraiseId("-1");
        }
        int saveFlag = appraiseInnerServiceSMOImpl.saveAppraise(appraisePo);
        if (saveFlag > 0) {
            if("10001".equals(appraisePo.getObjType())){//对维修单评价
                //1. 先找出之前的处理中repairUser 为了获取上一步的操作人
//                Map info = new HashMap();
//                info.put("repairId", appraisePo.getObjId());
//                info.put("staffId", appraisePo.getAppraiseUserId());
//                info.put("state", RepairUserDto.STATE_DOING);
//                info.put("statusCd", "0");
//               // List<Map> repairUserInfoMap = repairUserServiceDaoImpl.getRepairUserInfo(info);
                RepairUserDto repairUserDtoQuery = new RepairUserDto();
                repairUserDtoQuery.setRepairId(appraisePo.getObjId());
                repairUserDtoQuery.setStaffId(appraisePo.getAppraiseUserId());
                repairUserDtoQuery.setState(RepairUserDto.STATE_DOING);
                List<RepairUserDto> repairUserDtos = repairUserInnerServiceSMOImpl.queryRepairUsers(repairUserDtoQuery);
                Assert.listOnlyOne(repairUserDtos,"没有处理中的维修单或存在多条处理中");
                String preStaffId = repairUserDtos.get(0).getPreStaffId();
                String preStaffName = repairUserDtos.get(0).getPreStaffName();

                //2. 评价后修改用户进度状态 处理中->评价完成
//                info = new HashMap();
//                info.put("repairId", appraisePo.getObjId());
//                info.put("staffId", appraisePo.getAppraiseUserId());
//                info.put("preState", RepairUserDto.STATE_DOING);
//                info.put("state", RepairUserDto.STATE_APPRAISE_DONE);
//                info.put("endTime", DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A));
//                info.put("context", appraisePo.getContext());
//                info.put("statusCd", "0");
                RepairUserDto repairUserDto = new RepairUserDto();
                repairUserDto.setRepairId(appraisePo.getObjId());
                repairUserDto.setStaffId(appraisePo.getAppraiseUserId());
                repairUserDto.setPreState(RepairUserDto.STATE_DOING);
                repairUserDto.setState(RepairUserDto.STATE_APPRAISE_DONE);
                repairUserDto.setEndTime(new Date());
                repairUserDto.setContext(appraisePo.getContext());
                repairUserInnerServiceSMOImpl.updateRepairUser(repairUserDto);

                //3. 根据配置文件 判断是否回访
                //3.1 获取维修单 为了拿repairType
//                info = new HashMap();
//                info.put("repairId", appraisePo.getObjId());
//                List<Map> repairInfoMap = repairServiceDaoImpl.getRepairInfo(info);

                RepairDto repairDto = new RepairDto();
                repairDto.setRepairId(appraisePo.getObjId());
                List<RepairDto> repairDtos = repairInnerServiceSMOImpl.queryRepairs(repairDto);
                Assert.listOnlyOne(repairDtos,"维修单不正确或有多条记录");

                //3.2 获取维修配置信息
//                info = new HashMap();
//                info.put("repairType", repairDtos.get(0).getRepairType());
//                List<Map> repairSettingInfoMap = repairSettingServiceDaoImpl.getRepairSettingInfo(info);
                RepairSettingDto repairSettingDto = new RepairSettingDto();
                repairSettingDto.setRepairType(repairDtos.get(0).getRepairType());
                List<RepairSettingDto> repairSettingDtos = repairSettingInnerServiceSMOImpl.queryRepairSettings(repairSettingDto);

                Assert.listOnlyOne(repairSettingDtos,"维修类型配置不正确或有多条记录");
                String returnVisitFlag = repairSettingDtos.get(0).getReturnVisitFlag();

                RepairPoolPo repairPoolPo = new RepairPoolPo();
                repairPoolPo.setRepairId(appraisePo.getObjId());
//                Map repairInfo = new HashMap();
//                repairInfo.put("repairId", appraisePo.getObjId());
//                repairInfo.put("statusCd", "0");
                //如果回访 新增一条 repairUser staffId=员工 state=10001 处理中的数据  context=电话回访
                if("003".equals(returnVisitFlag)){
                    //1. busniess增加一条
                    RepairUserDto addRepairUserInfo = new RepairUserDto();
                    addRepairUserInfo.setbId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_bId));
                   // addRepairUserInfo.put("operate", "ADD");
                    addRepairUserInfo.setContext("电话回访");
                    addRepairUserInfo.setRepairId(appraisePo.getObjId());
                    addRepairUserInfo.setRuId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_ruId));
                    addRepairUserInfo.setState(RepairUserDto.STATE_DOING);
                    addRepairUserInfo.setCommunityId( repairDtos.get(0).getCommunityId());
                    addRepairUserInfo.setStaffId(preStaffId);//传到上一步的人那里
                    addRepairUserInfo.setStaffName(preStaffName);
                    addRepairUserInfo.setPreStaffId(appraisePo.getAppraiseUserId());
                    addRepairUserInfo.setPreStaffName(appraisePo.getAppraiseUserName());
                    addRepairUserInfo.setStartTime(new Date());
                    addRepairUserInfo.setRepairEvent(RepairUserDto.REPAIR_EVENT_RETURN_VISIT_USER);
                    repairUserInnerServiceSMOImpl.saveRepairUserBusiness(addRepairUserInfo);
                    //2. Instance增加一条
                    repairUserInnerServiceSMOImpl.saveRepairUserInstance(addRepairUserInfo);

                    //待回访
                   // repairInfo.put("state", RepairDto.STATE_RETURN_VISIT);
                    repairPoolPo.setState(RepairDto.STATE_RETURN_VISIT);
                }else{
                    //如果不回访 维修单办理完成

                    //办理完成
                    //repairInfo.put("state", RepairDto.STATE_COMPLATE);
                    repairPoolPo.setState(RepairDto.STATE_COMPLATE);
                }

                repairInnerServiceSMOImpl.updateRepair(repairPoolPo);
                //repairServiceDaoImpl.updateRepairInfoInstance(repairInfo);
            }

            return ResultVo.createResponseEntity(ResultVo.CODE_OK, ResultVo.MSG_OK);
        }
        return ResultVo.createResponseEntity(ResultVo.CODE_OK, ResultVo.MSG_OK);
    }
}
